package com.ycm.serializers;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.ycm.enums.BaseEnum;

import java.io.IOException;

/**
 * 枚举序列化器
 *
 * @author ycm
 * @date 2024-07-11
 */
public class CustomEnumSerializer extends JsonSerializer<BaseEnum> {
    @Override
    public void serialize(BaseEnum baseEnum, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeNumber(baseEnum.getValue());
        // 增加一个字段，格式为【枚举类名称+EnumDesc】，存储枚举的desc
        String currentName = jsonGenerator.getOutputContext().getCurrentName();
        jsonGenerator.writeStringField(currentName + "EnumDesc", baseEnum.desc());
    }

}
